﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Licensed MIT: http://www.opensource.org/licenses/mit-license.php
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Web.Script.Serialization;

namespace DNA.Mvc
{
    [Serializable,Table("dna_WidgetDescriptors")]
    public class WidgetDescriptor
    {
        [Key]
        [Column("ID", Order = 1)]
        public int ID { get; set; }

        [MaxLength(255)]
        public string Action { get; set; }

        [MaxLength(1024)]
        public string Area { get; set; }

        [MaxLength(50)]
        public string ContentType { get; set; }

        [MaxLength(2048)]
        public string ContentUrl { get; set; }

        [MaxLength(255)]
        public string Controller { get; set; }

        [Column(TypeName="ntext")]
        public string Defaults { get; set; }

        [Column(TypeName = "ntext")]
        public string Description { get; set; }

        [MaxLength(1024)]
        public string IconUrl { get; set; }

        [MaxLength(1024)]
        public string ImageUrl { get; set; }

        [MaxLength(1024)]
        public string InstalledPath { get; set; }

        public bool? IsClosable { get; set; }

        public bool? IsDeletable { get; set; }

        public int Scopes { get; set; }

        public bool ShowBorder { get; set; }

        public bool? ShowHeader { get; set; }
        
        [MaxLength(50)]
        public string Title { get; set; }

        [MaxLength(1024)]
        public string TitleLinkUrl { get; set; }

        [MaxLength(50)]
        public string Version { get; set; }

        public DateTime Modified { get; set; }

        [ScriptIgnore]
        public virtual ICollection<WidgetInstance> Widgets { get; set; }

        private Dictionary<string, object> properties = null;

        [NotMapped, ScriptIgnore]
        public Dictionary<string, object> Properties
        {
            get
            {
                if (properties == null)
                {
                    properties = new Dictionary<string, object>();
                    if (!string.IsNullOrEmpty(this.Defaults))
                        properties = (new JavaScriptSerializer()).Deserialize<Dictionary<string, object>>(this.Defaults);
                }
                return properties;
            }
            set
            {
                if (value != null)
                    this.Defaults = (new JavaScriptSerializer()).Serialize(value);
                else
                    this.Defaults = "{}";
            }
        }

        [NotMapped]
        [ScriptIgnore]
        public string ControllerShortName
        {
            get
            {
                if (!string.IsNullOrEmpty(this.Controller))
                {
                    var type = Type.GetType(this.Controller);
                    if (type != null)
                        return type.Name.Replace("Controller", "");
                }
                return this.Controller;
            }
        }
    }
}
